package com.cndw.kungfu.model.player;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.config.ConstantsDailyItem;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

/**
 * 玩家每天的记录数据
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-7-4 下午4:23:38
 */
public class PlayerEveryDay implements Serializable {

	/**
	 * 正式上线后不能修改
	 */
	private static final long serialVersionUID = 1019677889002L;
	/**
	 * 当前完成任务次数
	 */
	private Map<Integer, Integer> taskTimesMap = new HashMap<Integer, Integer>(6);
	/**
	 * 进入副本次数
	 */
	private Map<Integer, Integer> undercityTimesMap = new HashMap<Integer, Integer>();
	/**
	 * 上次cacheId
	 */
	private int cacheId;
	/**
	 * 通用int数据
	 */
	private Map<String, Integer> intDataMap = new HashMap<String, Integer>();
	/**
	 * 祝福的好友
	 */
	private Set<Long> blessSet = new HashSet<Long>();
	/**
	 * 最后重置时间
	 */
	private int lastTime;
	/**
	 * 是否领取每日奖励
	 */
	private boolean logAward;
	/**
	 * 今日在线时间
	 */
	private int onLine;
	/**
	 * pvp次数
	 */
	private transient int pveBug;
	/**
	 * 战斗次数
	 */
	private int bn;
	
	private transient long playerId;

	public PlayerEveryDay() {
		lastTime = TimeProcesser.getUnixTime();
	}
	public PlayerEveryDay(long playerId){
		lastTime = TimeProcesser.getUnixTime();
		this.playerId = playerId;
	}

	public int intDataGet(String key) {
		if (intDataMap.containsKey(key)) {
			return intDataMap.get(key);
		}
		return 0;
	}

	public void intDataSet(String key, int value) {
		intDataMap.put(key, value);
	}

	public void intDataAlter(String key, int value) {
		intDataMap.put(key, intDataGet(key) + value);
	}

	public void intDataRemove(String key) {
		intDataMap.remove(key);
	}

	public boolean intDataStatContants(String key, int checkType) {
		return (intDataGet(key) & checkType) > 0;
	}
	
	public void intDataBitStatAdd(String key,int checkType){
		checkType = intDataGet(key) | checkType;
		intDataSet(key, checkType);
	}
	

	public int getLastTime() {
		return lastTime;
	}

	/**
	 * 增加一次任务次数
	 * 
	 * @param type
	 */
	public synchronized void alterTaskTimes(int type) {
		taskTimesMap.put(type, getTaskTimes(type) + 1);
	}

	public int getTaskTimes(int type) {
		if (taskTimesMap.containsKey(type)) {
			return taskTimesMap.get(type);
		} else {
			return 0;
		}
	}

	/**
	 * 增加副本进入次数
	 * 
	 * @param undercityId
	 */
	public void underCityTimesIncrement(int undercityId) {
		int finalTimes = getUndercityTimes(undercityId) + 1;
		undercityTimesMap.put(undercityId, finalTimes);
	}

	public int getUndercityTimes(int undercityId) {
		if (undercityTimesMap.containsKey(undercityId)) {
			return undercityTimesMap.get(undercityId);
		} else {
			return 0;
		}
	}

	/**
	 * 获得每日修为
	 */
	public int getDailyScore() {
		return intDataGet(ConstantsDayIntKey.dailyScore);
	}

	public int getScoreAward() {
		return intDataGet(ConstantsDayIntKey.scoreAward);
	}

	public void addScoreAward(int scoreType) {
		intDataSet(ConstantsDayIntKey.scoreAward, intDataGet(ConstantsDayIntKey.scoreAward) | (1 << scoreType));
	}

	public boolean isScoreAward(int scoreType) {
		return (intDataGet(ConstantsDayIntKey.scoreAward) & (1 << scoreType)) > 0;
	}

	public int getDailyItem() {
		return intDataGet(ConstantsDayIntKey.dailyItem);
	}

	public boolean isLogAward() {
		return logAward;
	}

	public void setLogAward(boolean logAward) {
		this.logAward = logAward;
	}

	public boolean isNeedReset(long uid) {
		if (playerId == 0) {
			playerId = uid;
		}
		return lastTime < TimeProcesser.getMorningTime();
	}

	public int getCacheId() {
		return cacheId;
	}

	public void setCacheId(int cacheId) {
		this.cacheId = cacheId;
	}

	public Set<Long> getBlessSet() {
		if (blessSet == null) {
			intiBlessSet();
		}
		return blessSet;
	}

	private synchronized void intiBlessSet() {
		if (blessSet == null) {
			blessSet = new HashSet<Long>();
		}
	}

	public void finishDailyItem(int itemType) {
		try {
			int item = (1 << itemType);
			if ((getDailyItem() & item) <= 0) {
				intDataSet(ConstantsDayIntKey.dailyItem, getDailyItem() | item);
				int oldScore = getDailyScore();
				intDataSet(ConstantsDayIntKey.dailyScore, oldScore + ConstantsDailyItem.itemScore[itemType]);
				
				Context.getGeneralServer().chkAwardActive(playerId, oldScore, getDailyScore());
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 修复分数问题
	 */
	public void fixedDailyScore(){
		int dailyItem = getDailyItem();
		int score = 0;
		for(int i = 0; i < ConstantsDailyItem.itemScore.length; i++){
			int itemType = i;
			if((dailyItem & (1 << itemType)) > 0){
				score+= ConstantsDailyItem.itemScore[i];
			}
		}
		intDataSet(ConstantsDayIntKey.dailyScore,score);
	}

	public int getPveBug() {
		return pveBug;
	}

	public void setPveBug(int pveBug) {
		this.pveBug = pveBug;
	}

	public void alterPveBug(int alter) {
		pveBug += alter;
	}

	public int getBattleNum() {
		return bn;
	}

	public void setBattleNum(int battleNum) {
		this.bn = battleNum;
	}

	public void alterBattleNum(int alter) {
		bn += alter;
	}

	public int getOnLine() {
		return onLine;
	}

	public void setOnLine(int onLine) {
		this.onLine = onLine;
	}

}
